import * as types from '../constants/ActionTypes';

function updateMessageConfirm(state, key, value){
    
    let { allmessage, viewmessage, wxmessid } = state;

    if (allmessage && allmessage.length && wxmessid && key) {
        for (let i = 0, len = allmessage.length; i < len; i++) {
            let messageItem = allmessage[i];
            if (messageItem.id === wxmessid) {
                messageItem[key] = value;
            }
        }
    }
    if (viewmessage && viewmessage.length && wxmessid && key) {
        for (let j = 0, len = viewmessage.length; j < len; j++) {
            let messageItem = viewmessage[j];
            if (messageItem.id === wxmessid) {
                messageItem[key] = value;
            }
        }
    }
}

function updateMessageProductStatus(state, pid, status){
    let { allmessage, viewmessage } = state;
    if (allmessage && allmessage.length && pid && status) {
        for (let i = 0, len = allmessage.length; i < len; i++) {
            let messageItem = allmessage[i];
            if (messageItem.gsid === pid) {
                messageItem.viewstatus = status;
            }
        }
    }
    if (viewmessage && viewmessage.length && pid && status) {
        for (let j = 0, len = viewmessage.length; j < len; j++) {
            let messageItem = viewmessage[j];
            if (messageItem.gsid === pid) {
                messageItem.viewstatus = status;
            }
        }
    }
}

const initState = {
    message_status : 0, 
    viewmessage_status : 0,
    message : '加载中', 
    allmessage : [],
    viewmessage : []
};

export default function domessage(state = initState, action = {}) {
    const {
        type, payload
    } = action;

    switch (type) {
        //============================================================
        //获取消息列表
        //============================================================
        case types.FETCH_MESSAGES:

            let start_all = payload.start_all;
            if (start_all <= 1) {
                state.allmessage = [];
            }
            return { ...state, ...payload };

        case types.FETCH_MESSAGES_SUCCESS:
            let successInfo = {
                message_status : 1,
                message : "获取消息成功",
                allmessage : state.allmessage.concat(payload.results)
            }
            return {...state, ...successInfo};
        case types.FETCH_MESSAGES_FAILURE:
            let failureInfo = {
                message_status : -1,
                message : "获取消息失败",
                allmessage : [...state.allmessage]
            }
            return {...state, failureInfo};

        //============================================================
        //获取已关注消息列表
        //============================================================
        case types.FETCH_VIEWMESSAGES:

           let start_view = payload.start_view;
            if (start_view <= 1) {
                state.viewmessage = [];
            }
            return { ...state , ...payload};

        case types.FETCH_VIEWMESSAGES_SUCCESS:
            let viewSuccessInfo = {
                viewmessage_status : 1,
                message : "获取消息成功",
                viewmessage : state.viewmessage.concat(payload.results)
            }
            return {...state, ...viewSuccessInfo};
        case types.FETCH_VIEWMESSAGES_FAILURE:

            let viewFailureInfo = {
                viewmessage_status : -1,
                message : "获取消息失败",
                viewmessage : [...state.viewmessage]
            }
            return {...state, viewFailureInfo};

        case types.RESET_MESSAGES:
            return { ...payload } ;

        //============================================================
        //中线操作买入查看
        //============================================================
        case types.CONFIRM_MESSAGE:
            state.wxmessid = payload.wxmessid;
            return { ...state };

        case types.CONFIRM_MESSAGE_SUCCESS:
            let confirmInfo = null;
            if (payload.results && payload.results.length) {
                updateMessageConfirm(state, "msgconfirm", "2");
                confirmInfo = {
                    message_confirm_status : 1,
                    message : "消息查看成功"
                };
            }else{
                confirmInfo = {
                    message_confirm_status : -1,
                    message : "消息查看失败"
                };
            }

            return {...state, ...confirmInfo};
        case types.CONFIRM_MESSAGE_FAILURE:
            let confirmFailureInfo = {
                message_confirm_status : -1,
                message : payload.message
            };
            return {...state, ...confirmFailureInfo};

        //============================================================
        //更新消息所属策略关注状态
        //============================================================
        case types.UPDATE_MESSAGE_PRODUCT_STATUS:

            let { pid, status } = payload;

            updateMessageProductStatus(state, pid, status);

            let updateData = {
                update_message_product_status : 1,
                message : "更新成功"
            };

            return { ...state, ...updateData };
        default:
            return state;
    }
}